#include "groupmodel.hpp"
#include "db.hpp"
#include "group.hpp"
#include <muduo/base/Logging.h>

using namespace std;

static std::string server = "127.0.0.1";
static std::string dbuser = "root";
static std::string dbpwd = "123456";
static std::string dbname = "chat";


bool GroupModel::createGroup(Group &group)
{
    char sql[1024] = { 0 };
    MySQL mysql;
    bool ret;

    do {
        ret = false;
        sprintf(sql, "insert into allgroup(groupname, groupdesc) values('%s', '%s')", 
            group.getName().c_str(), group.getDesc().c_str());
        if (!mysql.connect(server, dbuser, dbpwd, dbname)) {
            group.setId(mysql_insert_id(mysql.getConnection()));
            break;
        }
        ret = mysql.update(sql);
        
    } while (0);

    return ret;
}

bool GroupModel::addGroup(int userid, int groupid, const std::string &role)
{
    char sql[1024] = { 0 };
    MySQL mysql;
    bool ret;

    do {
        ret = false;
        sprintf(sql, "insert into groupuser values('%d', '%d', '%s')", 
            groupid, userid, role.c_str());
        if (!mysql.connect(server, dbuser, dbpwd, dbname)) {
            break;
        }
        ret = mysql.update(sql);
    } while (0);

    return ret;
}

std::vector<Group> GroupModel::queryGroups(int userid)
{
    char sql[1024] = { 0 };
    MYSQL_RES *res;
    MySQL mysql;
    MYSQL_ROW row;
    vector<Group> groupList;

    do {
        res = nullptr;
        row = nullptr;
        sprintf(sql, "select a.id, a.groupname, a.groupdesc from allgroup a inner join \
            groupuser b on a.id = b.groupid where b.userid = %d", userid);
        if (mysql.connect(server, dbuser, dbpwd, dbname)) {
            res = mysql.query(sql);
            if (!res) {
                LOG_ERROR << "failed to query user" << userid <<  "group ";
                break;
            }

            while (row = mysql_fetch_row(res)) {
                Group group;
                group.setId(atoi(row[0]));
                group.setName(row[1]);
                group.setDesc(row[2]);
                groupList.push_back(group);
            }
            if (res) {
                mysql_free_result(res);
            }

            for (Group &group : groupList) {
                sprintf(sql, "select a.id, a.name, a.state, b.grouprole from user a inner join \
                    groupuser b on b.userid = a.id where b.groupid = %d", group.getId());
                res = mysql.query(sql);
                while (row = mysql_fetch_row(res)) {
                    GroupUser user;
                    user.setId(atoi(row[0]));
                    user.setName(row[1]);
                    user.setState(row[2]);
                    user.setRole(row[3]);
                    group.getUsers().push_back(user);
                }
                if (res) {
                    mysql_free_result(res);
                }
            }
        }
    } while (0);

 
    return groupList;
}

std::vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    char sql[1024] = { 0 };
    MYSQL_RES *res;
    MySQL mysql;
    MYSQL_ROW row;
    vector<int> groupUserIds;

    do {
        res = nullptr;
        row = nullptr;
        sprintf(sql, "select userid from groupuser where groupid = '%d' and userid != '%d'", groupid, userid);
        if (mysql.connect(server, dbuser, dbpwd, dbname)) {
            res = mysql.query(sql);
            if (!res) {
                LOG_ERROR << "failed to query user " << userid;
                break;
            }

            while (row = mysql_fetch_row(res)) {
                groupUserIds.push_back(atoi(row[0]));
            }
            if (res) {
                mysql_free_result(res);
            }
        }
    } while (0);
}